package com.sskj.uboex.presenter;

import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.StockCoinBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.uboex.bean.Banner;
import com.sskj.uboex.bean.NoticeList;
import com.sskj.uboex.http.HttpConfig;
import com.sskj.uboex.ui.fragment.HangqingFragment;

import java.util.ArrayList;
import java.util.List;


public class HangqingFragmentPresenter extends BasePresenter<HangqingFragment> {
    public void getData(String code, boolean isArea) {
        httpService.getProduct(isArea ? null : code)
                .execute(new JsonCallBack<HttpData<List<StockCoinBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<StockCoinBean>>> response) {
                        HttpData<List<StockCoinBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateCoin(httpData.getData());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<List<StockCoinBean>>> response) {
                        mView.refreshFail();
                        super.onError(response);
                    }
                });
    }

    public void onePay(String num) {
        httpService.onePay(num)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                    }
                });
    }

    public void getRMB() {
        httpService.getRMB()
                .execute(new JsonCallBack<HttpData<String>>() {
                    @Override
                    public void onSuccess(Response<HttpData<String>> response) {
                        HttpData<String> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {

                            mView.updateRMB(httpData.getData());
                        }

                    }
                });
    }

    public void getBannerUrl() {
        httpService.getBannerImage().execute(new JsonCallBack<HttpData<Banner>>() {
            @Override
            public void onSuccess(Response<HttpData<Banner>> response) {
                HttpData<Banner> httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {
                    if (httpData.getData() != null) {
                        mView.showBanner(response.body().getData());
                    }
                }
            }
        });
    }

    public void getNotice() {
        httpService.getGGList(null, null)
                .execute(new JsonCallBack<HttpData<PageBean<NoticeList>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<PageBean<NoticeList>>> response) {
                        HttpData<PageBean<NoticeList>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            if (httpData.getData() != null && httpData.getData().getList() != null) {
                                List<NoticeList> list = httpData.getData().getList();
                                List<NoticeList> listNew = new ArrayList<>();
                                switch (list.size()) {
                                    case 0:
                                        break;
                                    case 1:
                                        listNew = list.subList(0, 1);
                                        break;
                                    case 2:
                                        listNew = list.subList(0, 2);
                                        break;
                                    default:
                                        listNew = list.subList(0, 3);
                                        break;
                                }
                                mView.setNotice(listNew);
                            }
                        }

                    }
                });
    }
}
